package patrol.mobile.com.mobilepatrol.activity.bridge.regular;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.dl7.recycler.listener.OnRecyclerViewItemLongClickListener;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.BridgeRegularBrokenListAdapter;
import patrol.mobile.com.mobilepatrol.bean.QldqjcBjqsb;
import patrol.mobile.com.mobilepatrol.manager.QManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.widget.dialog.AppleDialog;

/**
 * Created by apple on 17/9/29.
 * 桥梁定期检查缺损列表
 */

public class BridgeRegularCheckBrokenListActivity extends BaseActivity {
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    private BaseQuickAdapter<QldqjcBjqsb> mAdapter;
    private List<QldqjcBjqsb> mList = new ArrayList<>();
    private String qlcode;
    private String qldqjcid;

    @Override
    protected int attachLayoutRes() {
        return R.layout.comment_list;
    }

    @Override
    public void setListener() {
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(BridgeRegularCheckBrokenListActivity.this,BridgeRegularCheckBrokenDetailActivity.class);
                intent.putExtra("qlcode",qlcode);
                intent.putExtra("qldqjcid",qldqjcid);
                intent.putExtra("id",mList.get(position).id);
                startActivityForResult(intent,100);
                CommonUtil.openNewActivityAnim(BridgeRegularCheckBrokenListActivity.this,false);
            }
        });
        mAdapter.setOnItemLongClickListener(new OnRecyclerViewItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, final int position) {

                final AppleDialog dialog = new AppleDialog(BridgeRegularCheckBrokenListActivity.this,R.style.Dialog,"确认删除？");
                dialog.show();
                dialog.setmListener(new AppleDialog.SureListener() {
                    @Override
                    public void onSure() {
                        Logger.d(position);
                        QManager.delBridgebjqs(mList.get(position).id);
                        mList = QManager.getBridgeDQbjqsList(qldqjcid);
                        mAdapter.updateItems(mList);

                        dialog.dismiss();

                        //桥梁
                        //桥梁定期检查
                        //桥梁缺损
                    }
                });


                return false;
            }
        });
    }
    @Override
    public void initView() {
        //super的重要性
        super.initView();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BridgeRegularBrokenListAdapter(BridgeRegularCheckBrokenListActivity.this, mList);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mList = QManager.getBridgeDQbjqsList(qldqjcid);
        mAdapter.updateItems(mList);
    }

    @Override
    public void initData() {
        super.initData();

        qldqjcid = getIntent().getStringExtra("qldqjcid");
        qlcode = getIntent().getStringExtra("qlcode");
        mList = QManager.getBridgeDQbjqsList(qldqjcid);
        mAdapter.updateItems(mList);

    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CommonUtil.exitActivityAndBackAnim(BridgeRegularCheckBrokenListActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("桥梁定期检查缺损列表");
        tv_right_title.setText("添加");
        tv_right_title.setVisibility(View.VISIBLE);
        tv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //新增定期检查缺损列表obj
                Intent intent = new Intent(BridgeRegularCheckBrokenListActivity.this,BridgeRegularCheckBrokenDetailActivity.class);
                intent.putExtra("qlcode",qlcode);
                intent.putExtra("qldqjcid",qldqjcid);
                startActivityForResult(intent,100);
                CommonUtil.openNewActivityAnim(BridgeRegularCheckBrokenListActivity.this,false);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==100){
            mList = QManager.getBridgeDQbjqsList(qldqjcid);
            mAdapter.updateItems(mList);
        }
    }
}
